package com.goods.business.service.imp;

import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 牛俊杰
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

  @Autowired
  private ProductCategoryMapper productCategoryMapper;

  @Autowired
  private ProductMapper productMapper;

  /**
   * 分类树形结构
   * @param pageNum
   * @param pageSize
   * @return
   */
  @Override
  public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
    List<ProductCategoryVO> productCategoryVOList = findAll();
    List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
    if(!CollectionUtils.isEmpty(productCategoryVOList)){
      for (ProductCategoryVO productCategoryVO : productCategoryVOList) {
        ProductCategoryTreeNodeVO p = new ProductCategoryTreeNodeVO();
        BeanUtils.copyProperties(productCategoryVO,p);
        productCategoryTreeNodeVOS.add(p);
      }
    }
    List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
    List<ProductCategoryTreeNodeVO> page;
    if(pageSize!=null&&pageNum!=null){
      page = ListPageUtils.page(productCategoryTreeNodeVOList, pageSize, pageNum);
      return new PageVO<>(productCategoryTreeNodeVOList.size(),page);
    }else {
      return new PageVO<>(productCategoryTreeNodeVOList.size(), productCategoryTreeNodeVOList);
    }
  }

  /**
   * 查询所有分类
   * @return
   */
  @Override
  public List<ProductCategoryVO> findAll() {
    List<ProductCategory> productCategories = productCategoryMapper.selectAll();
    List<ProductCategoryVO> productCategoryVOS = new ArrayList<>();
    if(!CollectionUtils.isEmpty(productCategories)){
      for (ProductCategory productCategory : productCategories) {
        ProductCategoryVO p = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,p);
        productCategoryVOS.add(p);
      }
    }
    return productCategoryVOS;
  }

  /**
   * 查询父分类
   * @return
   */
  @Override
  public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
    List<ProductCategoryVO> productCategoryVOList = this.findAll();
    List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
    if(!CollectionUtils.isEmpty(productCategoryVOList)){
      for (ProductCategoryVO productCategoryVO : productCategoryVOList) {
        ProductCategoryTreeNodeVO p = new ProductCategoryTreeNodeVO();
        BeanUtils.copyProperties(productCategoryVO,p);
        productCategoryTreeNodeVOS.add(p);
      }
    }
    return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
  }

  /**
   * 添加分类信息
   * @param productCategoryVO
   */
  @Override
  public void add(ProductCategoryVO productCategoryVO) {
    ProductCategory productCategory = new ProductCategory();
      BeanUtils.copyProperties(productCategoryVO,productCategory);
      productCategory.setCreateTime(new Date());
      productCategory.setModifiedTime(new Date());
      productCategoryMapper.insert(productCategory);
  }

  /**
   * 编辑分类信息
   * @param id
   * @return
   * @throws BusinessException
   */
  @Override
  public ProductCategoryVO edit(Long id) throws BusinessException {
    ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
    if (productCategory==null){
      throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"编辑的分类不存在");
    }
    ProductCategoryVO productCategoryVO = new ProductCategoryVO();
    BeanUtils.copyProperties(productCategory,productCategoryVO);
    return productCategoryVO;
  }

  /**
   * 修改分类信息
   * @param id
   * @param productCategoryVO
   */
  @Override
  public void update(Long id,ProductCategoryVO productCategoryVO) {
    ProductCategory productCategory = new ProductCategory();
    BeanUtils.copyProperties(productCategoryVO,productCategory);
    productCategory.setModifiedTime(new Date());
    productCategoryMapper.updateByPrimaryKeySelective(productCategory);
  }

  /**
   * 删除分类信息
   * @param id
   * @throws BusinessException
   */
  @Override
  public void delete(Long id) throws BusinessException {
    ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
    if (productCategory==null){
      throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"分类未找到");
    }else {
      //判断是否有子分类
      Example p = new Example(ProductCategory.class);
      p.createCriteria().andEqualTo("pid",id);
      int child = productCategoryMapper.selectCountByExample(p);
      if (child!=0){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"存在子分类,无法删除");
      }
      //判断是否有物资引用
      Example p1 = new Example(Product.class);
      p1.createCriteria().andEqualTo("oneCategoryId",id).orEqualTo("twoCategoryId",id).orEqualTo("threeCategoryId",id);
      int product = productMapper.selectCountByExample(p1);
      if (product!=0){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该分类有物资引用,无法删除");
      }
      productCategoryMapper.deleteByPrimaryKey(id);
    }

  }
}
